<template>
  <div class="app-container">
    <template>
      <el-row>
        <el-col :span="24">
          <div class="h_top">
            <div class="search_box">
              <el-form :model="query" :inline="true">
                <el-row :gutter="10">
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">编 号</div>
                      <el-input v-model="query.coding" size="mini" @keyup.enter.native="search" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">名 称</div>
                      <el-input v-model="query.name" size="mini" @keyup.enter.native="search" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">资 产</div>
                      <v-select-s v-model="query.equipment_id" action="eqList" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">优先程度</div>
                      <el-select v-model="query.priority" size="mini">
                        <el-option v-for="(item,priorityIndex) in priority" :key="priorityIndex" :label="item" :value="priorityIndex" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">专 业</div>
                      <el-select v-model="query.profession" size="mini">
                        <el-option v-for="(item,professionIndex) in profession" :key="professionIndex" :label="item" :value="professionIndex" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">周 期</div>
                      <el-input v-model="query.period" size="mini" @keyup.enter.native="search" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">负责人</div>
                      <v-select-s v-model="query.admin_id" action="adminList" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">类 型</div>
                      <el-select v-model="query.type" size="mini">
                        <el-option v-for="(item, typeIndex) in typeCategory" :key="typeIndex" :value="typeIndex" :label="item" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">状 态</div>
                      <el-select v-model="query.status" size="mini">
                        <el-option v-for="(item, commonIndex) in commonStatus" :key="commonIndex" :label="item" :value="commonIndex" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="4">
                    <el-form-item>
                      <div class="search_label">准备（天数）</div>
                      <el-input v-model="query.ready" size="mini" @keyup.enter.native="search" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="2">
                    <el-form-item>
                      <div class="search_label">操 作</div>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="search">查找</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div class="toolBars">
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-document-add" size="mini" @click="addHandler">增加</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-tickets" size="mini" @click="exportHandler">导出</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-delete" :disabled="!selection.length" size="mini" @click="remove">删除</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reloadHandler">刷新</el-button>
              </div>
              <div class="fr">总数：<span>{{ total }}</span></div>
            </div>
          </div>
        </el-col>
      </el-row>
    </template>
    <div>
      <el-table v-loading="loading" :data="planList" size="small" stripe border :header-cell-style="{background:'#eef1f6',color:'#606266'}" style="width: 100%" @selection-change="selectionHandler">
        <el-table-column type="index" label="序号" min-width="4%" align="center" />
        <el-table-column type="selection" min-width="4%" align="center" />
        <el-table-column prop="coding" label="编号" min-width="8%" align="center" />
        <el-table-column prop="name" label="名称" min-width="8%" />
        <el-table-column prop="type" label="类别" min-width="4%" align="center" :formatter="typeFormat" />
        <el-table-column prop="profession" label="专业" min-width="8%" align="center" :formatter="professionFormat" />
        <el-table-column prop="priority" label="优先程度" min-width="8%" align="center" :formatter="priorityFormat" />
        <el-table-column prop="admin.name" label="负责人" min-width="8%" align="center" />
        <el-table-column prop="next_date" label="下一个日期" min-width="12%" align="center" />
        <el-table-column prop="period" label="周期" min-width="8%" align="center" :formatter="periodFormat" />
        <el-table-column prop="status" label="状态" min-width="8%" align="center" :formatter="statusFormat" />
        <el-table-column prop="approve" label="审批" min-width="8%" align="center" :formatter="approveFormat" />
        <el-table-column v-if="listHandle" fixed="right" label="操作" min-width="18" align="center">
          <template scope="scope">
            <el-button type="primary" size="mini" @click="updateHandler(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="confirmDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>
<script>
import API from '@/api/project/plan'
import { profession, priority, commonStatus, typeCategory } from '@/utils/options'
import VSelect from '@/components/select'
import xlsx from 'xlsx'
import exportMethod from '@/mixins/export'
export default {
  inject: ['reload', 'goUrl', 'delOne', 'delMany', 'getCommonStatusFormat', 'getApproveStatusFormat', 'formatDate'],
  components: {
    VSelect
  },
  mixins: [exportMethod],
  data() {
    return {
      loading: false,
      total: 0,
      query: { page: 1, limit: 15 },
      planList: [],
      selection: [],
      profession,
      priority,
      commonStatus,
      typeCategory
    }
  },
  computed: {
    tableMaxHeight() {
      return window.innerHeight - 260 + 'px'
    },
    listHandle() {
      return this.$store.state.settings.listHandle
    }
  },
  watch: {},
  created() {
    this.index()
  },
  methods: {
    search() {
      this.query.page = 1
      this.index()
    },
    selectionHandler(selection) {
      this.selection = selection
    },
    index() {
      this.loading = true
      const _this = this
      API.planList(this.query).then(response => {
        this.total = response.data.total

        response.data.data.forEach(function(e) {
          const execute_date = e.execute_date
          const period = e.period
          const next_date = new Date(execute_date).getTime() + period * 24 * 3600 * 1000
          e.next_date = _this.formatDate(next_date)
        })

        this.planList = response.data.data
      }).finally(() => {
        this.loading = false
      })
    },
    exportHandler() {
      const data = [
        [
          '编号',
          '名称',
          '单位',
          '资产',
          '优先程度',
          '专业',
          '执行时间',
          '周期',
          '成本代码',
          '负责人',
          '类型',
          '状态',
          '准备（天数）',
          '验证人员',
          '验证时间',
          '排序'
        ]
      ]
      let priority = ''
      let profession = ''
      let type = ''
      let status = ''
      let v_name = ''
      let v_date = ''

      if (this.selection.length === 0) {
        this.$message.error('请选择要到导出的数据')
        return false
      } else {
        this.selection.forEach((item) => {
          if (item.priority === 0) {
            priority = '所有'
          } else if (item.priority === 1) {
            priority = '48小时内'
          } else if (item.priority === 2) {
            priority = '超过48小时'
          } else if (item.priority === 3) {
            priority = '立即'
          }

          if (item.profession === 0) {
            profession = '所有'
          } else if (item.profession === 1) {
            profession = '电气'
          } else if (item.profession === 2) {
            profession = '机械'
          }

          if (item.type === 0) {
            type = '系统'
          } else {
            type = '自定义'
          }

          if (item.status === 0) {
            status = '启用'
          } else if (item.status === 1) {
            status = '停用'
          }
          if (item.verify_admin) {
            v_name = item.verify_admin.name
            v_date = item.verify_date
          } else {
            v_name = '未审核'
            v_date = '未审核'
          }

          data.push([
            item.coding,
            item.name,
            item.unit,
            item.eq.name,
            priority,
            profession,
            item.execute_date,
            item.period,
            item.cost.coding,
            item.admin.name,
            type,
            status,
            item.ready,
            v_name,
            v_date,
            item.sort
          ])
        })
      }
      const workbook = {
        SheetNames: ['projectPlan'],
        Sheets: {
          'projectPlan': xlsx.utils.aoa_to_sheet(data)
        }
      }
      this.exportExec(workbook, '更新维护计划数据报表.xlsx')
    },
    remove() {
      const selection = this.selection
      this.delMany(API, selection)
    },
    confirmDel(id) {
      this.delOne(API, id, false)
    },
    priorityFormat(row) {
      if (row.priority === 0) {
        return '所有'
      } else if (row.priority === 1) {
        return '延误48小时以内'
      } else if (row.priority === 2) {
        return '延误超过48小时'
      } else if (row.priority === 3) {
        return '立即'
      }
    },
    typeFormat(row) {
      if (row.type === 0) {
        return '系统'
      } else if (row.type === 1) {
        return '自定义'
      }
    },
    professionFormat(row) {
      if (row.profession === 0) {
        return '所有'
      } else if (row.profession === 1) {
        return '电气'
      } else if (row.profession === 1) {
        return '机械'
      }
    },
    periodFormat(row) {
      return row.period + '天'
    },
    addHandler() {
      this.goUrl('/project/plan/plan_add')
    },
    updateHandler(id) {
      const url = `/project/plan/plan_edit/${id}`
      this.goUrl(url)
    },
    reloadHandler() {
      this.reload()
    },
    statusFormat(row) {
      return this.getCommonStatusFormat(row)
    },
    approveFormat(row) {
      return this.getApproveStatusFormat(row)
    }
  }
}
</script>
